package com.tbit.uqbike.webmanager.controller;


import com.tbit.uqbike.constant.SysConstant;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.RideLog;
import com.tbit.uqbike.object.pojo.TerPosition;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.base.HistoryService;
import com.tbit.uqbike.service.business.RideLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * 历史轨迹
 *
 * @author Leon
 * 2019年11月11日 下午10:00:01
 */
@Validated
@RestController
@RequestMapping(value = "/history", produces = BaseController.MEDIA_JSON_UTF8)
public class HistoryController extends BaseController {
    @Autowired
    private HistoryService historyService;
    @Autowired
    private RideLogService rideLogService;

    /* 根据时间点获取历史轨迹 */
    @RequestMapping("/getByTime")
    public JsonResult getByTime(@NotNull(message = "ver.userCode.notnull") String userCode,
                                @NotNull(message = "ver.startTime.notnull") Long startTime,
                                @NotNull(message = "ver.endTime.notnull") Long endTime,
                                Integer mapType, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }
        List<TerPosition> terPositions = historyService.getByUserCodeAndTime(loginInfo, mapType, userCode, startTime, endTime);
        return JsonResult.ok(terPositions);
    }

    /* 获取最后一次骑行轨迹*/
    @RequestMapping("/getLastRideHistory")
    public JsonResult getLastRideHistory(@NotNull(message = "ver.userCode.notnull") String userCode, Integer mapType, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        List<TerPosition> terPositions = historyService.getLastRideHistory(loginInfo, mapType, userCode);

        return JsonResult.ok(terPositions);
    }

    /* 获取最后非用户骑行轨迹*/
    @RequestMapping("/getLastActiveHistory")
    public JsonResult getLastActiveHistory(@NotNull(message = "ver.userCode.notnull") String userCode,
                                           Integer mapType, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }

        List<TerPosition> terPositions = historyService.getLastActiveHistory(loginInfo, mapType, userCode);

        return JsonResult.ok(terPositions);
    }

    /**
     * 通过订单编号获取订单轨迹
     *
     * @param orderNO 订单编号
     * @param mapType 地图类型
     * @param request
     * @return
     */
    @RequestMapping("/getByOrderNO")
    public JsonResult getByOrderNO(@NotNull(message = "ver.orderNO.notNull") String orderNO,
                                   Integer mapType, HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        if (mapType == null) {
            mapType = SysConstant.GOOGLE;
        }
        RideLog rideLog = rideLogService.getByOrderNO(orderNO);
        if (rideLog == null) {
            throw new BaseException("order_does_not_exist");
        }
        if (rideLog.getEndTime() == null) {
            rideLog.setEndtime(new Date());
        }
        List<TerPosition> terPositions = historyService.getByUserCodeAndTime(loginInfo, mapType, rideLog.getUserCode(), rideLog.getStartTime().getTime(), rideLog.getEndTime().getTime());

        return JsonResult.ok(terPositions);
    }
}